home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gsimage.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  16.2 KB  |  545 lines

  1. /* Copyright (C) 1996, 1997, 1998, 1999, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gsimage.c,v 1.3 2000/09/19 19:00:29 lpd Exp $ */
  20. /* Image setup procedures for Ghostscript library */
  21. #include "memory_.h"
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gsstruct.h"
  25. #include "gscspace.h"
  26. #include "gsmatrix.h"        /* for gsiparam.h */
  27. #include "gsimage.h"
  28. #include "gxarith.h"        /* for igcd */
  29. #include "gxdevice.h"
  30. #include "gxiparam.h"
  31. #include "gxpath.h"        /* for gx_effective_clip_path */
  32. #include "gzstate.h"
  33.  
  34.  
  35. /*
  36.   The main internal invariant for the gs_image machinery is
  37.   straightforward.  The state consists primarily of N plane buffers
  38.   (planes[]).
  39. */
  40. typedef struct image_enum_plane_s {
  41. /*
  42.   The state of each plane consists of:
  43.  
  44.   - A row buffer, aligned and (logically) large enough to hold one scan line
  45.     for that plane.  (It may have to be reallocated if the plane width or
  46.     depth changes.)  A row buffer is "full" if it holds exactly a full scan
  47.     line.
  48. */
  49.     gs_string row;
  50. /*
  51.   - A position within the row buffer, indicating how many initial bytes are
  52.     occupied.
  53. */
  54.     uint pos;
  55. /*
  56.   - A (retained) source string, which may be empty (size = 0).
  57. */
  58.     gs_const_string source;
  59. } image_enum_plane_t;
  60. /*
  61.   The possible states for each plane do not depend on the state of any other
  62.   plane.  Either:
  63.  
  64.   - pos = 0, source.size = 0.
  65.  
  66.   - If the underlying image processor says the plane is currently wanted,
  67.     either:
  68.  
  69.     - pos = 0, source.size >= one full row of data for this plane.  This
  70.       case allows us to avoid copying the data from the source string to the
  71.       row buffer if the client is providing data in blocks of at least one
  72.       scan line.
  73.  
  74.     - pos = full, source.size may have any value.
  75.  
  76.     - pos > 0, pos < full, source.size = 0;
  77.  
  78.   - If the underlying image processor says the plane is not currently
  79.     wanted:
  80.  
  81.     - pos = 0, source.size may have any value.
  82.  
  83.   This invariant holds at the beginning and end of each call on
  84.   gs_image_next_planes.  Note that for each plane, the "plane wanted" status
  85.   and size of a full row may change after each call of plane_data.  As
  86.   documented in gxiparam.h, we assume that a call of plane_data can only
  87.   change a plane's status from "wanted" to "not wanted", or change the width
  88.   or depth of a wanted plane, if data for that plane was actually supplied
  89.   (and used).
  90. */
  91.  
  92. /* Define the enumeration state for this interface layer. */
  93. /*typedef struct gs_image_enum_s gs_image_enum; *//* in gsimage.h */
  94. struct gs_image_enum_s {
  95.     /* The following are set at initialization time. */
  96.     gs_memory_t *memory;
  97.     gx_device *dev;        /* if 0, just skip over the data */
  98.     gx_image_enum_common_t *info;    /* driver bookkeeping structure */
  99.     int num_planes;
  100.     int height;
  101.     bool wanted_varies;
  102.     /* The following are updated dynamically. */
  103.     int plane_index;        /* index of next plane of data, */
  104.                 /* only needed for gs_image_next */
  105.     int y;
  106.     bool error;
  107.     byte wanted[gs_image_max_planes]; /* cache gx_image_planes_wanted */
  108.     byte client_wanted[gs_image_max_planes]; /* see gsimage.h */
  109.     image_enum_plane_t planes[gs_image_max_planes]; /* see above */
  110.     /*
  111.      * To reduce setup for transferring complete rows, we maintain a
  112.      * partially initialized parameter array for gx_image_plane_data_rows.
  113.      * The data member is always set just before calling
  114.      * gx_image_plane_data_rows; the data_x and raster members are reset
  115.      * when needed.
  116.      */
  117.     gx_image_plane_t image_planes[gs_image_max_planes];
  118. };
  119.  
  120. gs_private_st_composite(st_gs_image_enum, gs_image_enum, "gs_image_enum",
  121.             gs_image_enum_enum_ptrs, gs_image_enum_reloc_ptrs);
  122. #define gs_image_enum_num_ptrs 2
  123.  
  124. /* GC procedures */
  125. private 
  126. ENUM_PTRS_WITH(gs_image_enum_enum_ptrs, gs_image_enum *eptr)
  127. {
  128.     /* Enumerate the data planes. */
  129.     index -= gs_image_enum_num_ptrs;
  130.     if (index < eptr->num_planes)
  131.     ENUM_RETURN_STRING_PTR(gs_image_enum, planes[index].source);
  132.     index -= eptr->num_planes;
  133.     if (index < eptr->num_planes)
  134.     ENUM_RETURN_STRING_PTR(gs_image_enum, planes[index].row);
  135.     return 0;
  136. }
  137. ENUM_PTR(0, gs_image_enum, dev);
  138. ENUM_PTR(1, gs_image_enum, info);
  139. ENUM_PTRS_END
  140. private RELOC_PTRS_WITH(gs_image_enum_reloc_ptrs, gs_image_enum *eptr)
  141. {
  142.     int i;
  143.  
  144.     RELOC_PTR(gs_image_enum, dev);
  145.     RELOC_PTR(gs_image_enum, info);
  146.     for (i = 0; i < eptr->num_planes; i++)
  147.     RELOC_CONST_STRING_PTR(gs_image_enum, planes[i].source);
  148.     for (i = 0; i < eptr->num_planes; i++)
  149.     RELOC_STRING_PTR(gs_image_enum, planes[i].row);
  150. }
  151. RELOC_PTRS_END
  152.  
  153. /* Create an image enumerator given image parameters and a graphics state. */
  154. int
  155. gs_image_begin_typed(const gs_image_common_t * pic, gs_state * pgs,
  156.              bool uses_color, gx_image_enum_common_t ** ppie)
  157. {
  158.     gx_device *dev = gs_currentdevice(pgs);
  159.     gx_clip_path *pcpath;
  160.     int code = gx_effective_clip_path(pgs, &pcpath);
  161.  
  162.     if (code < 0)
  163.     return code;
  164.     if (uses_color)
  165.     gx_set_dev_color(pgs);
  166.     return gx_device_begin_typed_image(dev, (const gs_imager_state *)pgs,
  167.         NULL, pic, NULL, pgs->dev_color, pcpath, pgs->memory, ppie);
  168. }
  169.  
  170. /* Allocate an image enumerator. */
  171. private void
  172. image_enum_init(gs_image_enum * penum)
  173. {
  174.     /* Clean pointers for GC. */
  175.     penum->info = 0;
  176.     penum->dev = 0;
  177.     penum->plane_index = 0;
  178.     penum->num_planes = 0;
  179. }
  180. gs_image_enum *
  181. gs_image_enum_alloc(gs_memory_t * mem, client_name_t cname)
  182. {
  183.     gs_image_enum *penum =
  184.     gs_alloc_struct(mem, gs_image_enum, &st_gs_image_enum, cname);
  185.  
  186.     if (penum != 0) {
  187.     penum->memory = mem;
  188.     image_enum_init(penum);
  189.     }
  190.     return penum;
  191. }
  192.  
  193. /* Start processing an ImageType 1 image. */
  194. int
  195. gs_image_init(gs_image_enum * penum, const gs_image_t * pim, bool multi,
  196.           gs_state * pgs)
  197. {
  198.     gs_image_t image;
  199.     gx_image_enum_common_t *pie;
  200.     int code;
  201.  
  202.     image = *pim;
  203.     if (image.ImageMask) {
  204.     image.ColorSpace = NULL;
  205.     if (pgs->in_cachedevice <= 1)
  206.         image.adjust = false;
  207.     } else {
  208.     if (pgs->in_cachedevice)
  209.         return_error(gs_error_undefined);
  210.     if (image.ColorSpace == NULL)
  211.         image.ColorSpace =
  212.         gs_cspace_DeviceGray((const gs_imager_state *)pgs);
  213.     }
  214.     code = gs_image_begin_typed((const gs_image_common_t *)&image, pgs,
  215.                 image.ImageMask | image.CombineWithColor,
  216.                 &pie);
  217.     if (code < 0)
  218.     return code;
  219.     return gs_image_enum_init(penum, pie, (const gs_data_image_t *)&image,
  220.                   pgs);
  221. }
  222.  
  223. /*
  224.  * Return the number of bytes of data per row for a given plane.
  225.  */
  226. inline uint
  227. gs_image_bytes_per_plane_row(const gs_image_enum * penum, int plane)
  228. {
  229.     const gx_image_enum_common_t *pie = penum->info;
  230.  
  231.     return (pie->plane_widths[plane] * pie->plane_depths[plane] + 7) >> 3;
  232. }
  233.  
  234. /* Cache information when initializing, or after transferring plane data. */
  235. private void
  236. cache_planes(gs_image_enum *penum)
  237. {
  238.     int i;
  239.  
  240.     if (penum->wanted_varies) {
  241.     penum->wanted_varies =
  242.         !gx_image_planes_wanted(penum->info, penum->wanted);
  243.     for (i = 0; i < penum->num_planes; ++i)
  244.         if (penum->wanted[i])
  245.         penum->image_planes[i].raster =
  246.             gs_image_bytes_per_plane_row(penum, i);
  247.         else
  248.         penum->image_planes[i].data = 0;
  249.     }
  250. }
  251. /* Advance to the next wanted plane. */
  252. private void
  253. next_plane(gs_image_enum *penum)
  254. {
  255.     int px = penum->plane_index;
  256.  
  257.     do {
  258.     if (++px == penum->num_planes)
  259.         px = 0;
  260.     } while (!penum->wanted[px]);
  261.     penum->plane_index = px;
  262. }
  263. /*
  264.  * Initialize plane_index and (if appropriate) wanted and
  265.  * wanted_varies at the beginning of a group of planes.
  266.  */
  267. private void
  268. begin_planes(gs_image_enum *penum)
  269. {
  270.     cache_planes(penum);
  271.     penum->plane_index = -1;
  272.     next_plane(penum);
  273. }
  274.  
  275. /* Start processing a general image. */
  276. int
  277. gs_image_enum_init(gs_image_enum * penum, gx_image_enum_common_t * pie,
  278.            const gs_data_image_t * pim, gs_state *pgs)
  279. {
  280.     return gs_image_common_init(penum, pie, pim, pgs->memory,
  281.                 (pgs->in_charpath ? NULL :
  282.                  gs_currentdevice_inline(pgs)));
  283. }
  284. int
  285. gs_image_common_init(gs_image_enum * penum, gx_image_enum_common_t * pie,
  286.         const gs_data_image_t * pim, gs_memory_t * mem, gx_device * dev)
  287. {
  288.     int i;
  289.  
  290.     if (pim->Width == 0 || pim->Height == 0) {
  291.     gx_image_end(pie, false);
  292.     return 1;
  293.     }
  294.     image_enum_init(penum);
  295.     penum->memory = mem;
  296.     penum->dev = dev;
  297.     penum->info = pie;
  298.     penum->num_planes = pie->num_planes;
  299.     /*
  300.      * Note that for ImageType 3 InterleaveType 2, penum->height (the
  301.      * expected number of data rows) differs from pim->Height (the height
  302.      * of the source image in scan lines).  This doesn't normally cause
  303.      * any problems, because penum->height is not used to determine when
  304.      * all the data has been processed: that is up to the plane_data
  305.      * procedure for the specific image type.
  306.      */
  307.     penum->height = pim->Height;
  308.     for (i = 0; i < pie->num_planes; ++i) {
  309.     penum->planes[i].pos = 0;
  310.     penum->planes[i].source.size = 0;    /* for gs_image_next_planes */
  311.     penum->planes[i].row.data = 0; /* for GC */
  312.     penum->planes[i].row.size = 0; /* ditto */
  313.     penum->image_planes[i].data_x = 0; /* just init once, never changes */
  314.     }
  315.     /* Initialize the dynamic part of the state. */
  316.     penum->y = 0;
  317.     penum->error = false;
  318.     penum->wanted_varies = true;
  319.     begin_planes(penum);
  320.     return 0;
  321. }
  322.  
  323. /* Return the set of planes wanted. */
  324. const byte *
  325. gs_image_planes_wanted(gs_image_enum *penum)
  326. {
  327.     int i;
  328.  
  329.     /*
  330.      * A plane is wanted at this interface if it is wanted by the
  331.      * underlying machinery and has no buffered or retained data.
  332.      */
  333.     for (i = 0; i < penum->num_planes; ++i)
  334.     penum->client_wanted[i] =
  335.         (penum->wanted[i] &&
  336.          penum->planes[i].pos + penum->planes[i].source.size <
  337.            penum->image_planes[i].raster);
  338.     return penum->client_wanted;
  339. }
  340.  
  341. /*
  342.  * Return the enumerator memory used for allocating the row buffers.
  343.  * Because some PostScript files use save/restore within an image data
  344.  * reading procedure, this must be a stable allocator.
  345.  */
  346. private gs_memory_t *
  347. gs_image_row_memory(const gs_image_enum *penum)
  348. {
  349.     return gs_memory_stable(penum->memory);
  350. }
  351.  
  352. /* Free the row buffers when cleaning up. */
  353. private void
  354. free_row_buffers(gs_image_enum *penum, int num_planes, client_name_t cname)
  355. {
  356.     int i;
  357.  
  358.     for (i = num_planes - 1; i >= 0; --i) {
  359.     if_debug3('b', "[b]free plane %d row (0x%lx,%u)\n",
  360.           i, (ulong)penum->planes[i].row.data,
  361.           penum->planes[i].row.size);
  362.     gs_free_string(gs_image_row_memory(penum), penum->planes[i].row.data,
  363.                penum->planes[i].row.size, cname);
  364.     penum->planes[i].row.data = 0;
  365.     penum->planes[i].row.size = 0;
  366.     }
  367. }
  368.  
  369. /* Process the next piece of an image. */
  370. int
  371. gs_image_next(gs_image_enum * penum, const byte * dbytes, uint dsize,
  372.           uint * pused)
  373. {
  374.     int px = penum->plane_index;
  375.     int num_planes = penum->num_planes;
  376.     int i, code;
  377.     uint used[gs_image_max_planes];
  378.     gs_const_string plane_data[gs_image_max_planes];
  379.  
  380.     if (penum->planes[px].source.size != 0)
  381.     return_error(gs_error_rangecheck);
  382.     for (i = 0; i < num_planes; i++)
  383.     plane_data[i].size = 0;
  384.     plane_data[px].data = dbytes;
  385.     plane_data[px].size = dsize;
  386.     penum->error = false;
  387.     code = gs_image_next_planes(penum, plane_data, used);
  388.     *pused = used[px];
  389.     if (code >= 0)
  390.     next_plane(penum);
  391.     return code;
  392. }
  393.  
  394. int
  395. gs_image_next_planes(gs_image_enum * penum,
  396.              gs_const_string *plane_data /*[num_planes]*/,
  397.              uint *used /*[num_planes]*/)
  398. {
  399.     const int num_planes = penum->num_planes;
  400.     int i;
  401.     int code = 0;
  402.  
  403. #ifdef DEBUG
  404.     if (gs_debug_c('b')) {
  405.     int pi;
  406.  
  407.     for (pi = 0; pi < num_planes; ++pi)
  408.         dprintf6("[b]plane %d source=0x%lx,%u pos=%u data=0x%lx,%u\n",
  409.              pi, (ulong)penum->planes[pi].source.data,
  410.              penum->planes[pi].source.size, penum->planes[pi].pos,
  411.              (ulong)plane_data[pi].data, plane_data[pi].size);
  412.     }
  413. #endif
  414.     for (i = 0; i < num_planes; ++i) {
  415.         used[i] = 0;
  416.     if (penum->wanted[i] && plane_data[i].size != 0) {
  417.         penum->planes[i].source.size = plane_data[i].size;
  418.         penum->planes[i].source.data = plane_data[i].data;
  419.     }
  420.     }
  421.     for (;;) {
  422.     /* If wanted can vary, only transfer 1 row at a time. */
  423.     int h = (penum->wanted_varies ? 1 : max_int);
  424.  
  425.     /* Move partial rows from source[] to row[]. */
  426.     for (i = 0; i < num_planes; ++i) {
  427.         int pos, size;
  428.         uint raster;
  429.  
  430.         if (!penum->wanted[i])
  431.         continue;    /* skip unwanted planes */
  432.         pos = penum->planes[i].pos;
  433.         size = penum->planes[i].source.size;
  434.         raster = penum->image_planes[i].raster;
  435.         if (size > 0) {
  436.         if (pos < raster && (pos != 0 || size < raster)) {
  437.             /* Buffer a partial row. */
  438.             int copy = min(size, raster - pos);
  439.             uint old_size = penum->planes[i].row.size;
  440.  
  441.             /* Make sure the row buffer is fully allocated. */
  442.             if (raster > old_size) {
  443.             gs_memory_t *mem = gs_image_row_memory(penum);
  444.             byte *old_data = penum->planes[i].row.data;
  445.             byte *row =
  446.                 (old_data == 0 ?
  447.                  gs_alloc_string(mem, raster,
  448.                          "gs_image_next(row)") :
  449.                  gs_resize_string(mem, old_data, old_size, raster,
  450.                           "gs_image_next(row)"));
  451.  
  452.             if_debug5('b', "[b]plane %d row (0x%lx,%u) => (0x%lx,%u)\n",
  453.                   i, (ulong)old_data, old_size,
  454.                   (ulong)row, raster);
  455.             if (row == 0) {
  456.                 code = gs_note_error(gs_error_VMerror);
  457.                 free_row_buffers(penum, i, "gs_image_next(row)");
  458.                 break;
  459.             }
  460.             penum->planes[i].row.data = row;
  461.             penum->planes[i].row.size = raster;
  462.             }
  463.             memcpy(penum->planes[i].row.data + pos,
  464.                penum->planes[i].source.data, copy);
  465.             penum->planes[i].source.data += copy;
  466.             penum->planes[i].source.size = size -= copy;
  467.             penum->planes[i].pos = pos += copy;
  468.             used[i] += copy;
  469.         }
  470.         }
  471.         if (h == 0)
  472.         continue;    /* can't transfer any data this cycle */
  473.         if (pos == raster) {
  474.         /*
  475.          * This plane will be transferred from the row buffer,
  476.          * so we can only transfer one row.
  477.          */
  478.         h = min(h, 1);
  479.         penum->image_planes[i].data = penum->planes[i].row.data;
  480.         } else if (pos == 0 && size >= raster) {
  481.         /* We can transfer 1 or more planes from the source. */
  482.         h = min(h, size / raster);
  483.         penum->image_planes[i].data = penum->planes[i].source.data;
  484.         } else
  485.         h = 0;        /* not enough data in this plane */
  486.     }
  487.     if (h == 0 || code != 0)
  488.         break;
  489.     /* Pass rows to the device. */
  490.     if (penum->dev == 0) {
  491.         /*
  492.          * ****** NOTE: THE FOLLOWING IS NOT CORRECT FOR ImageType 3
  493.          * ****** InterleaveType 2, SINCE MASK HEIGHT AND IMAGE HEIGHT
  494.          * ****** MAY DIFFER (BY AN INTEGER FACTOR).  ALSO, plane_depths[0]
  495.          * ****** AND plane_widths[0] ARE NOT UPDATED.
  496.      */
  497.         if (penum->y + h < penum->height)
  498.         code = 0;
  499.         else
  500.         h = penum->height - penum->y, code = 1;
  501.     } else {
  502.         code = gx_image_plane_data_rows(penum->info, penum->image_planes,
  503.                         h, &h);
  504.         if_debug2('b', "[b]used %d, code=%d\n", h, code);
  505.         penum->error = code < 0;
  506.     }
  507.     /* Update positions and sizes. */
  508.     if (h == 0)
  509.         break;
  510.     for (i = 0; i < num_planes; ++i) {
  511.         int count;
  512.  
  513.         if (!penum->wanted[i])
  514.         continue;
  515.         count = penum->image_planes[i].raster * h;
  516.         if (penum->planes[i].pos) {
  517.         /* We transferred the row from the row buffer. */
  518.         penum->planes[i].pos = 0;
  519.         } else {
  520.         /* We transferred the row(s) from the source. */
  521.         penum->planes[i].source.data += count;
  522.         penum->planes[i].source.size -= count;
  523.         used[i] += count;
  524.         }
  525.     }
  526.     cache_planes(penum);
  527.     if (code > 0)
  528.         break;
  529.     }
  530.     /* Return the retained data pointers. */
  531.     for (i = 0; i < num_planes; ++i)
  532.     plane_data[i] = penum->planes[i].source;
  533.     return code;
  534. }
  535.  
  536. /* Clean up after processing an image. */
  537. void
  538. gs_image_cleanup(gs_image_enum * penum)
  539. {
  540.     free_row_buffers(penum, penum->num_planes, "gs_image_cleanup(row)");
  541.     if (penum->dev != 0)
  542.     gx_image_end(penum->info, !penum->error);
  543.     /* Don't free the local enumerator -- the client does that. */
  544. }
  545.